package com.memory.servlet;

import com.memory.dao.UserDao;
import com.memory.dao.impl.UserDaoImpl;
import com.memory.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet(urlPatterns = "/SelectById")
public class SelectServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//       创建Dao层
        UserDaoImpl userDao = new UserDaoImpl();
        User user = new User();
        ArrayList<User> userList = null;

        String Sname = request.getParameter("Sname");
        String Ssex = request.getParameter("Ssex");
        Integer Sage = Integer.parseInt(request.getParameter("Sage"));
        String Sdept = request.getParameter("Sdept");

        user.setSno(Integer.parseInt(request.getParameter("Sno")));
        user.setSname(Sname== null ? "" : request.getParameter("Sname"));
        user.setSsex(Ssex==null?"":Ssex);
        user.setSage(Sage==null?0:Sage);
        user.setSdept(Sdept==null?"":Sdept);

        try {
            userList = userDao.selectById(user);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        request.setAttribute("userList", userList);
        request.getRequestDispatcher("userList.jsp").forward(request, response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
